package com.tree.backend.config;

import com.tree.backend.interceptor.EnterpriseIdInterceptor;
import com.tree.backend.interceptor.JwtInterceptor;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@RequiredArgsConstructor
public class WebMvcConfig implements WebMvcConfigurer {

    private final JwtInterceptor jwtInterceptor;
    private final EnterpriseIdInterceptor enterpriseIdInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 先注册企业ID拦截器，确保在JWT拦截器之前执行
        registry.addInterceptor(enterpriseIdInterceptor)
                .addPathPatterns("/order/**")  // 只拦截订单相关接口
                .order(1);  // 设置拦截器顺序，数字越小优先级越高
        
        // 注册JWT拦截器
        registry.addInterceptor(jwtInterceptor)
                .addPathPatterns("/**")  // 拦截所有请求
                .excludePathPatterns("/login", "/register","/user","/enterprise")  // 排除登录和注册接口
                .order(2);  // JWT拦截器在企业ID拦截器之后执行
    }
}